import Vue from 'vue'
import VueRouter from 'vue-router'
import Reg from '../views/Reg.vue'
import login from '../views/login.vue'
import Main from '../views/Main.vue'
import store from '@/store/'
import Home from '../views/Menus/Home.vue'
import info from '../views/Menus/User/info.vue'
import UserAvatar from '../views/Menus/User/Avatar.vue'
import UserPwd from '../views/Menus/User/Pwd.vue'
import CrtList from '../views/Menus/Article/list.vue'
import Artlist from '../views/Menus/Article/Artlist.vue'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/reg', component: Reg },
    { path: '/login', component: login },
    {
      path: '/',
      component: Main,
      redirect: '/home',
      children: [
        {
          path: '/home',
          component: Home
        },
        {
          path: 'user-info',
          component: info
        },
        {
          path: 'user-avatar',
          component: UserAvatar
        },
        {
          path: '/user-pwd',
          component: UserPwd
        },
        {
          path: '/art-cate',
          component: CrtList
        },
        {
          path: '/art-list',
          component: Artlist
        }

      ]
    }

  ]
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login' || to.path === '/reg') {
    next()
  } else {
    const token = store.state.user.token
    if (token) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
